package com.wp.service.impl;

import com.wp.dao.UserDao;
import com.wp.domain.UserInfo;
import com.wp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 卫鹏
 * @Description
 * @createTime 2021年11月05日 19:02:00
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void register(UserInfo user) {
        userDao.register(user);
    }

    /**
     * 登录处理逻辑
     *
     * @param username 用户名
     * @return UserDetails
     * @throws UsernameNotFoundException 继承异常
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //调用dao中的查询username返回User对象的方法：
        UserInfo userInfo = userDao.findByUsername(username);
        //调用UserDetails的子类User处理权限逻辑：
        //将User对象返回：
        return new User(userInfo.getUserAccount(), userInfo.getPassword(), getAuthorities());
    }

    public List<SimpleGrantedAuthority> getAuthorities() {
        //创建List<SimpleGrantedAuthority>对象：
        List<SimpleGrantedAuthority> lists = new ArrayList<>();
        lists.add(new SimpleGrantedAuthority("ROLE_USER"));
        //将lists集合返回：
        return lists;
    }
}
